/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param head1 ListNode类 
     * @param head2 ListNode类 
     * @return ListNode类
     */
    ListNode* addInList(ListNode* head1, ListNode* head2) {
        vector<int> arr1;
        vector<int> arr2;
        ListNode* ans=nullptr;
        while(head1)
        {
            arr1.emplace_back(head1->val);
            head1=head1->next;
        }
        while(head2)
        {
            arr2.emplace_back(head2->val);
            head2=head2->next;
        }
        int p=0;
        int n=arr1.size()-1,m=arr2.size()-1;
        while(n>=0||m>=0||p>0)
        {
            int num1=n>=0?arr1[n--]:0;
            int num2=m>=0?arr2[m--]:0;
            int sum=num1+num2+p;
            p=sum/10;
            ListNode* tmp=new ListNode(sum%10);
            tmp->next=ans;
            ans=tmp;
        }

        return ans;
    }
};